﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Agile.Engine.View;
using System.Collections;
using Agile.Engine;
using Agile.Engine.Data;
using System.Data;

namespace Fis.Win.Rules
{
    public class LoginViewProvider : ClientViewProvider
    {
        public override Hashtable InitializeViewModel(Hashtable args)
        {
            return args ?? new Hashtable();
        }

        public override bool ExecuteValidate(Hashtable viewModel)
        {

            var cache = string.Format("{0}\\cache.sdf", ObjectContext.Environment.Config);

            //缓存存在
            if (System.IO.File.Exists(cache))
            {
                var da = new DataAccess() { Context = DataContext, TableName = DataTableName };
                var data = da.Query(Agile.Engine.Utils.DataUtils.ToFilter(viewModel));
                //不匹配
                if (data.Rows.Count == 0)
                {
                    return ServiceLogin(viewModel);
                }

                var state = Agile.Engine.Utils.DataUtils.Parse(0, data.Rows[0]["FSTATUS"]);
                //if (state < 0)
                //{
                //    View.Message = "用户未启用";
                //    return false;
                //}
                //else
                if (state < 0)
                {
                    View.Message = "无效用户";
                    return false;
                }

                Fis.Win.Utils.AppSession.IsAdminUser = data.Rows[0]["FRIGHTS"].Equals("1");
                Fis.Win.Utils.AppSession.BookID = viewModel["SBOOKID"].ToString();
                Fis.Win.Utils.AppSession.UserName = viewModel["FUSERNAME"].ToString();
                Fis.Win.Utils.AppSession.UserSID = data.Rows[0]["SID"].ToString();
                Fis.Win.Utils.AppSession.Password = viewModel["FPASSWORD"].ToString();
                var book = DataContext.DefaultModel.QueryTable("TBOOK");
                Fis.Win.Utils.AppSession.Book = Agile.Engine.Utils.DataUtils.ToHashTable(book.Rows[0]);
            }
            else
            {
                return ServiceLogin(viewModel);
            }

            return true;
        }

        bool ServiceLogin(Hashtable model)
        {
            var cache = string.Format("{0}\\cache.sdf", ObjectContext.Environment.Config);

            //访问服务
            var exec = InvokeService(model);
            View.Message = ServiceProcess.Message;

            //访问无效
            if (!exec)
            {
                return false;
            }

            //保存缓存账套
            if (ServiceProcess.Result.ContainsKey("cache"))
            {
                var binary = Agile.Engine.Utils.ZipUtils.UnZipBase64(ServiceProcess.Result["cache"].ToString());
                Agile.Engine.Utils.IOUtils.CreateDirectory(cache);
                Agile.Engine.Utils.IOUtils.WriteStream(binary, cache);
            }

            //登录成功
            if (ServiceProcess.Result.ContainsKey("USERSID"))
            {
                Fis.Win.Utils.AppSession.UserSID = ServiceProcess.Result["USERSID"].ToString();
            }

            //没有帐套，需要新建
            if (ServiceProcess.Result.ContainsKey("nobook"))
            {
                model["nobook"] = 1;
            }

            Fis.Win.Utils.AppSession.IsAdminUser = Agile.Engine.Utils.DataUtils.Parse(false, ServiceProcess.Result["IsAdmin"]);
            Fis.Win.Utils.AppSession.BookID = model["SBOOKID"].ToString();
            Fis.Win.Utils.AppSession.UserName = model["FUSERNAME"].ToString();
            Fis.Win.Utils.AppSession.Password = model["FPASSWORD"].ToString();

            return true;
        }

        public override bool UpdateLocal(Hashtable args)
        {
            var d = args["viewData"] as DataTable;

            DataAccess a = new DataAccess() { Context = DataContext, TableName = DataTableName };

            var r = d.Rows[0];
            ///没有启用服务，需要生成ID
            if (IsEnableService)
            {
            }

            if (View.ViewState == Agile.Engine.View.ViewState.AddNew)
            {
                r["SID"] = Guid.NewGuid().ToString();
                a.AddNew(r);
            }
            else
            {
                a.Update(r);
            }
            return true;
        }

    }
}
